///
/// transformer
///

import 'package:dio/dio.dart';
import './http_response.dart';
import './http_exception.dart';

abstract class HttpTransformer {
  HttpResponse parse(Response response);
}

class DefaultHttpTransformer extends HttpTransformer {
  @override
  HttpResponse parse(Response response) {
    final int data = response.data['data'];
    if (data == 401) {
      // token失效
      return HttpResponse.failureFromError(
        UnauthorisedException(message: "登录失效", code: response.statusCode)
      );
    }
    return HttpResponse.success(response.data);
  }
  //
  static final DefaultHttpTransformer _instance = DefaultHttpTransformer._internal();
  DefaultHttpTransformer._internal();
  factory DefaultHttpTransformer.getInstance() => _instance;
}